<?php

namespace App\Models;

// use Illuminate\Contracts\Auth\MustVerifyEmail;
use App\Enums\AssistantStatus;
use App\Enums\UserType;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Facades\Hash;
use Laravel\Sanctum\HasApiTokens;
use Laravel\Sanctum\NewAccessToken;

/**
 * @method static withTelephone(string $telephone)
 * @method static withStore($storeId)
 * @method static withType(\App\Enums\UserType $type)
 */
class User extends Authenticatable
{
    use HasApiTokens, HasFactory, Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'name', 'email', 'password',
        'telephone', 'avatar', 'store_id', 'last_login_time', 'status', 'remark', 'code', 'parent_user_id',
        'nickname', 'role_id', 'assistant_status', 'type', 'assistant_type_id', 'counter_work_start_at',
    ];

    /**
     * The attributes that should be hidden for serialization.
     *
     * @var array<int, string>
     */
    protected $hidden = [
        'password',
        'remember_token',
    ];

    /**
     * The attributes that should be cast.
     *
     * @var array<string, string>
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
        'password' => 'hashed',
        'assistant_status' => AssistantStatus::class,
    ];

    public function assistantType()
    {
        return $this->belongsTo(AssistantType::class);
    }

    public function parentUser()
    {
        return $this->belongsTo(User::class, 'parent_user_id', 'id');
    }

    public function role()
    {
        return $this->belongsTo(UserRole::class, 'role_id', 'id');
    }

    public function store()
    {
        return $this->belongsTo(Store::class);
    }

    public function handovers()
    {
        return $this->hasMany(Handover::class);
    }

    public function notificationRules()
    {
        return $this->hasMany(UserNotificationRule::class);
    }

    public function scopeWithTelephone($query, $telephone)
    {
        return $query->where('telephone', $telephone);
    }

    public function scopeWithStore($query, $storeId)
    {
        return $query->where('store_id', $storeId);
    }

    public function scopeWithType($query, UserType $type)
    {
        return $query->where('type', $type);
    }

    public function login($password) : NewAccessToken
    {
        if (!Hash::check($password, $this->password)) {
            throw new \Exception('密码错误');
        }

        $this->last_login_time = now();
        $this->save();

        $token = $this->createToken('user');

        return $token;
    }

    public function counterWork()
    {
        if (empty($this->counter_work_start_at)) {
            $this->counter_work_start_at = now();
            $this->save();
        }

        return $this;
    }

    public function counterEndWork()
    {
        $this->counter_work_start_at = null;
        $this->save();

        return $this;
    }
}
